home *** CD-ROM | disk | FTP | other *** search
- /*
- File: SampleLibrary.c
-
- Contains: Implementation of the CSampleLibrary shared library.
-
- Copyright: © 1993-1994 by Apple Computer, Inc., all rights reserved.
-
- */
-
-
- #include <LibraryManager.h>
- #include <LibraryManagerUtilities.h>
-
- #include <String.h>
- #include <Events.h>
- #include <QuickDraw.h>
- #include <Resources.h>
- #include <Menus.h>
- #include <Windows.h>
- #include <StdIO.h>
-
- #include "SampleLibrary.h" /* our traffic light library */
-
- /* GLOBALS */
-
- /* The "g" prefix is used to emphasize that a variable is global. */
-
- Boolean gLightState = false; /* state of the traffic light */
- WindowPtr gWindow; /* global traffic light window */
- Rect gStopRect; /* rectangle for stop light, set up by NewTrafficLight */
- Rect gGoRect; /* rectangle for go light, set up by NewTrafficLight */
-
- /* STATIC FUNCTIONS */
-
- static Boolean GoGetRect( short rectID, Rect *theRect);
-
- /*————————————————————————————————————————————————————————————————————————————————————
- GoGetRect
-
- loads the global rectrangles that are used for drawing traffic lights.
- ————————————————————————————————————————————————————————————————————————————————————*/
-
- static Boolean GoGetRect( short rectID, Rect *theRect)
- {
- Handle resource;
-
- resource = GetResource('RECT', rectID); /* get 'RECT' resource from library file */
- if ( resource != nil ) {
- *theRect = **((Rect**) resource);
- return true;
- }
- else
- return false;
- }
-
- /*————————————————————————————————————————————————————————————————————————————————————
- NewTrafficLight
-
- allocate trafficlight window and initilize the globals.
- ————————————————————————————————————————————————————————————————————————————————————*/
-
- OSErr NewTrafficLight()
- {
- long savedrefnum;
- OSErr err;
- MenuHandle themenu;
- TLibraryFile *libraryfile;
-
- Trace("NewTrafficLight\n"); /* send the output to trace monitor's window */
-
- gLightState = false;
-
- /* include the library's file in resource chain so we can allocate our menu
- and window resources */
-
- libraryfile = GetLocalLibraryFile();
-
- if( err = Preflight( libraryfile, &savedrefnum ) ) {
- Trace("NewTrafficLight failed to Preflight Error = %d\n", err );
- return err;
- }
-
- gWindow = GetNewWindow( rWindow, (Ptr) nil, (WindowPtr) -1 );
-
- GoGetRect(rStopRect, &gStopRect); /* get rectangle for Stop light */
- GoGetRect(rGoRect, &gGoRect); /* get rectangle for Go light */
-
- /* if there is a MENU resources in our shared library we should append it
- to our application */
-
- themenu = GetMenu( mLight ); /* get our traffic light menu */
- InsertMenu( themenu, 0 ); /* append to the application's menu */
-
- DrawMenuBar(); /* go ahead and update the menu bar */
-
- /* now we are done with accessing our library resource; restore the chain
- to it previous state */
-
- if( err = Postflight( libraryfile, savedrefnum ) )
- Trace("NewTrafficLight failed to Postflight error = %d\n", err );
-
- return err;
- }
-
- /*————————————————————————————————————————————————————————————————————————————————————
- FreeTrafficLight
-
- deallocate the memory used by trafficlight
- ————————————————————————————————————————————————————————————————————————————————————*/
-
- void FreeTrafficLight()
- {
- Trace("FreeTrafficLight\n"); /* tell Trace Monitor where we are */
- DisposeWindow(gWindow); /* application get ride of the window */
- }
-
- /*————————————————————————————————————————————————————————————————————————————————————
- GetLightState
-
- return current state of traffic light
- ————————————————————————————————————————————————————————————————————————————————————*/
-
- Boolean GetLightState()
- {
- Trace("GetLightState\n"); /* tell Trace Monitor where we are */
- return gLightState ; /* return the state of Traffic Light */
- }
-
- /*————————————————————————————————————————————————————————————————————————————————————
- SetLightState
-
- set the light to the new state
- ————————————————————————————————————————————————————————————————————————————————————*/
-
- void SetLightState( Boolean newState )
- {
- Trace("SetLightState\n"); /* tell Trace Monitor what we are doing */
- gLightState = newState; /* set the new state */
-
- SetPort(gWindow); /* make sure we are at the right port before */
- InvalRect(&gWindow->portRect); /* invalidating the update region */
- }
-
- /*————————————————————————————————————————————————————————————————————————————————————
- DrawLight
-
- draw the actual traffic light
- ————————————————————————————————————————————————————————————————————————————————————*/
-
- void DrawLight()
- {
- GrafPtr oldport;
-
- Trace("DrawLight\n"); /* tell the Trace Monitor what we are doing */
-
- if( gWindow ) {
-
- GetPort( &oldport ); /* save the old port */
- SetPort( gWindow ); /* set the new port to our object's port */
-
- EraseRect(&gWindow->portRect); /* clear out any garbage that may linger */
- if ( gLightState ) /* draw a red (or white) stop light */
- ForeColor(redColor);
- else
- ForeColor(whiteColor);
-
- PenSize( 2, 2 );
- PaintOval(&gStopRect);
- ForeColor(blackColor);
- FrameOval(&gStopRect);
- if ( ! gLightState ) /* draw a green (or white) go light */
- ForeColor(greenColor);
- else
- ForeColor(whiteColor);
- PaintOval(&gGoRect);
- ForeColor(blackColor);
- FrameOval(&gGoRect);
- PenSize( 1, 1 );
-
- SetPort( oldport ); /* restore the grafpointer */
- }
- }
-
- /*————————————————————————————————————————————————————————————————————————————————————
- AdjustTrafficLightMenus
-
- Enable and disable traffic light's menus based on the current state.
- ————————————————————————————————————————————————————————————————————————————————————*/
-
- void AdjustTrafficLightMenus( Boolean active )
- {
- long savedrefnum;
- MenuHandle menu;
- TLibraryFile *libraryfile;
- OSErr err;
-
- Trace("AdjustTrafficLightMenus\n"); /* tell the Trace Monitor where we are */
-
- libraryfile = GetLocalLibraryFile();
-
- /* include the library in resource chain so we can allocate our menu resources */
-
- if( err = Preflight( libraryfile, &savedrefnum ) ) {
- Trace("AdjustMenus failed to Preflight Error = %d\n", err );
- return;
- }
-
- if( menu = GetMenuHandle(mLight) ) { /* get the handle to traffic light menu */
-
- if ( active ) { /* do we need to enable them? */
- EnableItem(menu, iStop);
- EnableItem(menu, iGo);
- } else {
- DisableItem(menu, iStop);
- DisableItem(menu, iGo);
- }
-
- CheckItem(menu, iStop, gLightState); /* we can also determine check/uncheck state, too */
- CheckItem(menu, iGo, ! gLightState);
- }
- else
- Trace("GetMenuHandle returned NIL\n"); /* let Trace Monitor know we failed */
-
- /* restore the chain to its previous state */
- if( err = Postflight( libraryfile, savedrefnum ) )
- Trace("AdjustMenus failed to Postflight error = %d\n", err );
- }
-
-
- /*————————————————————————————————————————————————————————————————————————————————————
- DoTrafficLightMenuCommand
-
- Enable and disable menus based on the current state of traffic light.
- ————————————————————————————————————————————————————————————————————————————————————*/
-
- void DoTrafficLightMenuCommand( short menuItem )
- {
- long savedrefnum;
- TLibraryFile *libraryfile;
- OSErr err;
-
- Trace("DoTrafficLightMenuCommand\n");/* tell Trace Monitor what we are doing */
- /* include the library's file in resource chain */
- libraryfile = GetLocalLibraryFile();
- if( err = Preflight( libraryfile, &savedrefnum ) ) {
- Trace("AdjustMenus failed to Preflight Error = %d\n", err );
- return;
- }
-
- switch ( menuItem ) { /* what menu item selected? */
- case iStop:
- SetLightState( true ); /* set and update */
- break;
- case iGo:
- SetLightState( false ); /* set and update */
- break;
- }
- /* restore the chain to its previous state */
- if( err = Postflight( libraryfile, savedrefnum ) )
- Trace("AdjustMenus failed to Postflight error = %d\n", err );
- }
-